/**
 * Created by zhangxiaoyu on 16/12/09.
 */
angular.module('hyt.receiptService', ['ngResource'])

  .factory('ReceiptList', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getReceipts,
      receiptData={},pageSize=10;

    var resource = $resource(ApiUrl+':receiptSearch',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getReceiptData : function(receiptSearch){
        var hasNextPage = true;
        resource.query({
          receiptSearch:JSON.stringify({
            pageSize:'10',
            pageNo:'1',
            parameter:receiptSearch.parameter,
            userMsg:receiptSearch.userMsg
          })
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            if(r.rows.length < pageSize ){
              hasNextPage = false;
            }
            receiptData[pageSize] = {
              hasNextPage : hasNextPage,
              nextPage : 2,
              data : r.rows
            };
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('ReceiptList.Updated');
        })
      },
      getResult:function(){
        if(receiptData[pageSize]===undefined){
          return false;
        }
        return receiptData[pageSize].data;
      },
      getMoreReceiptData : function(receiptSearch){
        if(receiptData[pageSize] === undefined){
          return false;
        }
        var hasNextPage = receiptData[pageSize].hasNextPage;
        var nextPage = receiptData[pageSize].nextPage;
        var moreReceiptData = receiptData[pageSize].data;
        resource.query({
          receiptSearch:JSON.stringify({
            pageSize:'10',
            pageNo:nextPage.toString(),
            parameter:receiptSearch.parameter,
            userMsg:receiptSearch.userMsg
          })
        },function(r){
          console.log(r);
          nextPage++;
          if(r.rows.length < pageSize ){
            hasNextPage = false;
          }
          moreReceiptData = moreReceiptData.concat(r.rows);

          receiptData[pageSize] = {
            hasNextPage : hasNextPage,
            nextPage : nextPage,
            data : moreReceiptData
          };
          $rootScope.$broadcast('ReceiptList.Updated');
        })
      },
      hasNextPage: function() {
        if (receiptData[pageSize] === undefined) {
          return false;
        }
        return receiptData[pageSize].hasNextPage;
      }
    }
  })
  .factory('Recovery', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.recoveryReceipt,
      result;

    var resource = $resource(ApiUrl+':receiptId',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getRecovery : function(receiptId){
        console.log(receiptId);
        return resource.query({
          receiptId:receiptId
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('Recovery.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
